

"""
-------------------------------------------------
           0          1        2         3     
-------------------------------------------------
污染等级：  1         2         3
材料组：    1         2         3        4
类型：      操作者    患者
部位：      网电源    次级电路
重数：      1         2
电流类型：   DC       AC
-------------------------------------------------

"""
from math import ceil

from backend.table.Table12 import Table12_Process
from backend.table.Table16 import Table16_Process
from backend.table.Table13 import Table13_Process
from backend.table.Table14 import Table14_Process
from backend.table.Table15 import Table15_Process


def calculate_distance(pollution_class, 
              material, 
              object_type, 
              place_type, 
              multiple_num, 
              voltage_type, 
              peak_voltage, 
              supply_voltage, 
              altitude):
    
    
    if object_type == 1:    # 对患者(table 12)       
        """ 表12计算对患者的电气间隙和爬电距离 """
        ElecClearance, CreepDist = Table12_Process(multiple_num, voltage_type, peak_voltage)

    else:   # 对操作者
        """ 表16计算对操作者的爬电距离 """
        CreepDist = Table16_Process(pollution_class, material, multiple_num, peak_voltage)

        if place_type == 0:   # 网电源
            """ 表13计算网电源对操作者的电气间隙 """
            ElecClearance = Table13_Process(pollution_class, 
                                        multiple_num, 
                                        voltage_type, 
                                        peak_voltage, 
                                        supply_voltage)

            if peak_voltage > supply_voltage:
                """ 表14计算网电源的附加电气间隙，并相加 """
                ExtraElecClearance = Table14_Process(pollution_class, 
                                multiple_num, 
                                voltage_type, 
                                peak_voltage, 
                                supply_voltage)
                ElecClearance += ExtraElecClearance

        else:   # 次级电路
            """ 表15计算次级电路对操作者的电气间隙 """
            ElecClearance = Table15_Process(pollution_class, 
                                multiple_num, 
                                voltage_type, 
                                peak_voltage, 
                                supply_voltage)

    """ 表8计算海拔的影响 """
    if altitude <= 2000:
        ECratio = 1
    elif altitude > 2000 and altitude <= 3000:
        if object_type == 0:    
            ECratio = 1.14
        else:   
            ECratio = 1
    elif altitude > 3000 and altitude <= 4000:
        if object_type == 0:    
            ECratio = 1.29
        else:   
            ECratio = 1.14
    elif altitude > 4000 and altitude <= 5000:
        if object_type == 0:    
            ECratio = 1.48
        else:   
            ECratio = 1.29
    ElecClearance = ElecClearance * ECratio

    if CreepDist == "选用合适表格中的电气间隙":
        CreepDist = ElecClearance
        
    # 爬电距离 >= 电气间隙
    if CreepDist < ElecClearance:
        CreepDist = ElecClearance
        
    ElecClearance = ceil(ElecClearance*10) / 10
    CreepDist = ceil(CreepDist*10) / 10
    return ElecClearance, CreepDist # 电气间隙/爬电距离




